class Solution:
    def groupAnagrams(self, strs):
        res = {}
        for word in strs:
            word_tuple = list(word)
            word_tuple.sort()
            word_tuple = tuple(word_tuple)
            if word_tuple in res:
                res[word_tuple].append(word)
            else:
                res[word_tuple] = [word]
        return list(res.values())

    def groupAnagrams2(self, strs):
        res = {}
        word_dic = {"a": 2, "b": 3, "c": 5, "d": 7, "e": 11, "f": 13, "g": 17, "h": 19, "i": 23, "j": 29, "k": 31,
                    "l": 37, "m": 101, "n": 41, "o": 43, "p": 47, "q": 53, "r": 59, "s": 61, "t": 67, "u": 71, "v": 73,
                    "w": 79, "x": 83, "y": 89, "z": 97}
        for word in strs:
            ji = 1
            for s in word:
                ji *= word_dic[s]
            if ji in res:
                res[ji].append(word)
            else:
                res[ji] = [word]
        print(res)
        return list(res.values())

a = Solution()
print(a.groupAnagrams2(["eat", "tea", "tan", "ate", "nat", "bat"]))
